package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.LeaveApplicationMapper;
import com.hrma.dao.LeaveRecordMapper;
import com.hrma.entity.LeaveRecord;
import com.hrma.entity.LeaveRecordDTO;
import com.hrma.entity.LeaveRecordVO;
import com.hrma.entity.SelectResult;
import com.hrma.services.LeaveRecordService;
import com.hrma.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaveRecordImpl implements LeaveRecordService {
    private final LeaveRecordMapper leaveRecordMapper;

    public LeaveRecordImpl(LeaveRecordMapper leaveRecordMapper, LeaveApplicationMapper leaveApplicationMapper) {
        this.leaveRecordMapper = leaveRecordMapper;
        this.leaveApplicationMapper = leaveApplicationMapper;
    }

    private final LeaveApplicationMapper leaveApplicationMapper;

    @Override
    public LeaveRecordVO add(LeaveRecordVO data) {
        data.setAuditorId(UserUtil.getUserId());
        leaveRecordMapper.add(data);
        leaveApplicationMapper.setState(data.getState(),data.getApplicationId());
        return data;
    }

    @Override
    public List<SelectResult> select() {
        return leaveRecordMapper.select();
    }

    @Override
    public Page<LeaveRecordVO> getList(LeaveRecordDTO data) {
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        return leaveRecordMapper.getList(data);
    }

    @Override
    public Page<LeaveRecordVO> getPersonalList(LeaveRecordDTO data) {
        data.setEmployeeId(UserUtil.getUserId());
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        return leaveRecordMapper.getList(data);
    }
}
